//
//  SwitchViewController.m
//  View Switcher
//
//  Created by Jeff LaMarche on 7/6/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.


//<!!!BUGREPORT!!>>
//-openProfileformCreate dosen't work for certain reason. exiting the software and restarting will show the created profile.

#import "SwitchViewController.h";
#import "AdminMenuController.h";
#import "MainMenuController.h";
#import "ProfileMenuController.h";
#import "ViewController.h";
#import "CoinGameViewController.h";
#import "MathGameViewController.h";
#import "ProfileCreationController.h";
#import "TriviaController.h";
#import "Trivia.h"

@implementation SwitchViewController
@synthesize adminMenuController;
@synthesize mainMenuController;
@synthesize profileMenuController;
@synthesize viewController;
@synthesize coinGameViewController;
@synthesize mathgameViewController;
@synthesize profileCController;
@synthesize triviaController;
@synthesize trivia;

- (void)viewDidLoad
{
	MainMenuController *M = [MainMenuController alloc];
	self.mainMenuController = M;
	[M release];
	
	[mainMenuController.view addSubview:mainMenuController.Profile];
	[mainMenuController.view bringSubviewToFront:mainMenuController.Profile];
	mainMenuController.Profile.hidden = FALSE;
	[self.view insertSubview:mainMenuController.view atIndex:0];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
		// Initialization code
	}
	return self;
}

-(IBAction)startAtroid:(id)sender;
{
	[UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	if (self.viewController == nil)
	{
		ViewController *AController = 
		[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
		self.viewController = AController;
		[AController release];
	}
	[UIView setAnimationTransition:
	 UIViewAnimationTransitionFlipFromLeft
						   forView:self.view cache:YES];
	
	//[mainMenuController viewWillAppear:YES];
	[viewController viewWillDisappear:YES];
	[mainMenuController.view removeFromSuperview];
	[self.view insertSubview:viewController.view atIndex:0];
	[viewController viewDidDisappear:YES];
	//[mainMenuController viewDidAppear:YES];
	[UIView commitAnimations];
	
}

-(IBAction)startTrivia:(id)sender;
{
	[UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	if (self.triviaController == nil)
	{
		TriviaController *TController = 
		[[TriviaController alloc] initWithNibName:@"TriviaController" bundle:nil];
		self.triviaController = TController;
		[TController release];
	}
	[UIView setAnimationTransition:
	 UIViewAnimationTransitionFlipFromLeft
						   forView:self.view cache:YES];
	
	[mainMenuController viewWillAppear:YES];
	[triviaController viewWillDisappear:YES];
	[mainMenuController.view removeFromSuperview];
	[self.view insertSubview:triviaController.view atIndex:0];
	[triviaController viewDidDisappear:YES];
	[mainMenuController viewDidAppear:YES];
	[UIView commitAnimations];
	
}

-(IBAction)startCoin:(id)sender;
{
	[UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	if (self.coinGameViewController == nil)
	{
		CoinGameViewController *CController = 
		[[CoinGameViewController alloc] initWithNibName:@"CoinGameViewController" bundle:nil];
		self.coinGameViewController = CController;
		[CController release];
	}
	[UIView setAnimationTransition:
	 UIViewAnimationTransitionFlipFromLeft
						   forView:self.view cache:YES];
	
	[mainMenuController viewWillAppear:YES];
	[coinGameViewController viewWillDisappear:YES];
	[mainMenuController.view removeFromSuperview];
	[self.view insertSubview:coinGameViewController.view atIndex:0];
	[coinGameViewController viewDidDisappear:YES];
	[mainMenuController viewDidAppear:YES];
	[UIView commitAnimations];
	
}

-(IBAction)startSpeed:(id)sender;
{
	[UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	if (self.mathgameViewController == nil)
	{
		MathGameViewController	*MController = 
		[[MathGameViewController alloc] initWithNibName:@"MathGameViewController" bundle:nil];
		self.mathgameViewController = MController;
		[MController release];
	}
	[UIView setAnimationTransition:
	 UIViewAnimationTransitionFlipFromLeft
						   forView:self.view cache:YES];
	
	[mainMenuController viewWillAppear:YES];
	[mathgameViewController viewWillDisappear:YES];
	[mainMenuController.view removeFromSuperview];
	[self.view insertSubview:mathgameViewController.view atIndex:0];
	[mathgameViewController viewDidDisappear:YES];
	[mainMenuController viewDidAppear:YES];
	[UIView commitAnimations];
	
}


/*- (IBAction)openMainMenu:(id)sender
{
	[UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	if (self.mainMenuController == nil)
	{
		MainMenuController *mainController = 
		[[MainMenuController alloc] initWithNibName:@"MainMenu" bundle:nil];
		self.mainMenuController = mainController;
		[mainController release];
	}
	[UIView setAnimationTransition:
	 UIViewAnimationTransitionFlipFromLeft
						   forView:self.view cache:YES];
	
	[profileMenuController viewWillAppear:YES];
	[mainMenuController viewWillDisappear:YES];
	[profileMenuController.view removeFromSuperview];
	[self.view insertSubview:mainMenuController.view atIndex:0];
	[mainMenuController viewDidDisappear:YES];
	[profileMenuController viewDidAppear:YES];
	[UIView commitAnimations];
	
}*/

- (IBAction)openMainMenu:(id)sender
 {
	 
		 [UIView beginAnimations:@"View Flip" context:nil];
	 [UIView setAnimationDuration:1.25];
	 [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	 if (self.mainMenuController == nil)
	 {
		MainMenuController *mainController = 
		 [[MainMenuController alloc] initWithNibName:@"MainMenu" bundle:nil];
		
		 self.mainMenuController = mainController;
		 [mainController release];
	 }
	 [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:YES];
	
	 NSArray *subViews = self.view.subviews;
	 UIView *subview = [subViews objectAtIndex:0];
	
	 [mainMenuController viewWillDisappear:YES];
	 [subview removeFromSuperview];
	 [self.view insertSubview:mainMenuController.view atIndex:0];
	 [mainMenuController viewDidDisappear:YES];

	 [UIView commitAnimations];
	// [[subViews objectAtIndex:0]  removeFromSuperview];
	 //[self.view insertSubview:mainMenuController.view atIndex:0];
	
 
 }

- (IBAction)profileSelect:(id)sender
{
		if([[sender currentTitle] compare:@"Empty"] ){
						
			
			[self.mainMenuController.Profile removeFromSuperview];
			[self.view addSubview:mainMenuController.view];
			
			[mainMenuController setTitleNameDisplay:sender]; 
		
		}else{
			
			UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"That Profile Slot is Empty, Please Create a Profile Before Proceeding." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; 
		}
	
}




-(IBAction)openTrivia:(id)sender
{
	if (self.trivia == nil)
	{
		Trivia *T = 
		[[Trivia alloc] initWithNibName:@"Trivia" bundle:nil];
		self.trivia = T;
		[T release];
	}
	
	NSArray *subViews = self.view.subviews;
	[[subViews objectAtIndex:0]  removeFromSuperview];
	[self.view insertSubview:trivia.view atIndex:0];
	
}

- (IBAction)openProfileMenu
{
//	[self.mainMenuController.view removeFromSuperview];
	[mainMenuController.Profile setNeedsDisplay];
	[self.mainMenuController.view bringSubviewToFront:mainMenuController.Profile];
	[self.mainMenuController.view addSubview:mainMenuController.Profile];
	
	mainMenuController.Profile.hidden = FALSE;
	
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
    if (self.adminMenuController.view.superview == nil)
	{
        self.adminMenuController = nil;
	}
	else if(self.mainMenuController.view.superview == nil)
	{
		self.mainMenuController = nil;
	}
	else if(self.viewController.view.superview == nil)
	{
		self.viewController = nil;
	}
	else if(self.coinGameViewController.view.superview == nil)
	{
		self.coinGameViewController = nil;
	}
	else if(self.mathgameViewController.view.superview == nil)
	{
		self.mathgameViewController = nil;
	}
	else if(self.triviaController.view.superview == nil)
	{
		self.triviaController = nil;
	}
	else 
	{
		self.profileCController = nil;
	}

	
}
- (void)dealloc {
	[profileMenuController release];
	[mainMenuController release];
	[adminMenuController release];
	[viewController release];
	[coinGameViewController release];
	[mathgameViewController release];
	[profileCController release];
	[triviaController release];
	[super dealloc];
}

@end
